<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch">
      <el-form-item label="ID" prop="id">
        <el-input v-model="queryParams.id" placeholder="Pls input id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="Type" prop="type">
        <el-input v-model="queryParams.type" placeholder="Pls input Type" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--
      <el-form-item label="Year" prop="year">
        <el-input v-model="queryParams.year" placeholder="Pls input Year" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="Month" prop="month">
        <el-input v-model="queryParams.month" placeholder="Pls input Month" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      -->
      <el-form-item label="Status" prop="status">
        <el-select v-model="queryParams.status" placeholder="Select Status" clearable>
          <el-option v-for="dict in dict.type.status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="Source" prop="source">
        <el-input v-model="queryParams.source" placeholder="Pls input Source" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">Search</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">Reset</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ade:emr:add']">New</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ade:emr:edit']">edit</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ade:emr:remove']">Delete</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ade:emr:export']">Export</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-share" size="mini" @click="handleFormat">Format</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="emrList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="42" align="center" />
      <el-table-column label="ID" align="center" width="70" prop="id" />
      <el-table-column label="Year" align="center" width="60" prop="year" />
      <el-table-column label="Month" align="center" width="60" prop="month" />
      <el-table-column label="Type" align="center" width="100" prop="type" show-overflow-tooltip />
      <el-table-column label="Content" align="left" prop="content" show-overflow-tooltip />
      <el-table-column label="ADEs" align="left" prop="ades" show-overflow-tooltip />
      <el-table-column label="Status" align="center" width="100" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="Source" align="center" width="100" prop="source" show-overflow-tooltip />
      <!--
      <el-table-column label="Remark" align="center" width="150" prop="remark" show-overflow-tooltip />
      -->
      <el-table-column label="UpdateBy" align="center" width="80" prop="updateBy" show-overflow-tooltip />
      <el-table-column label="Update Time" align="center" width="140" prop="updateTime" />
      <el-table-column label="Actions" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ade:emr:edit']">Edit</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ade:emr:remove']">Delete</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改ADE Dataset对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="75%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">

        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item label="Year" prop="year">
              <el-input v-model="form.year" placeholder="Pls input Year" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="Month" prop="month">
              <el-input v-model="form.month" placeholder="Pls input Month" />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="Source" prop="source">
              <el-input v-model="form.source" placeholder="Pls input Source" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="Content" prop="content">
          <el-input v-model="form.content" rows="14" type="textarea" placeholder="" />
        </el-form-item>


        <el-form-item label="ADEs" prop="ades">
          <el-input v-model="form.ades" type="textarea" placeholder="" />
        </el-form-item>
        <el-form-item label="Status" prop="status">
          <el-select v-model="form.status" placeholder="Select Status">
            <el-option v-for="dict in dict.type.status" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="Remark" prop="remark">
          <el-input v-model="form.remark" type="textarea" rows="2" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmr, getEmr, delEmr, addEmr, updateEmr,startFormat } from "@/api/ade/emr";

export default {
  name: "Emr",
  dicts: ['status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // ADE Dataset表格数据
      emrList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        content: null,
        year: null,
        month: null,
        type: null,
        ades: null,
        status: null,
        source: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        content: [
          { required: true, message: "Content can not be empty!", trigger: "blur" }
        ],
        status: [
          { required: true, message: "Status can not be empty!", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //ADE标注语料进行格式化
    handleFormat(){
      this.$modal.confirm('Are you sure to submit the format task?').then(() => {
        return startFormat();
      }).then(() => {
        this.$modal.msgSuccess("The format has commenced. Please be patient and wait...");
        this.getList();
      }).catch(() => { });
    },
    /** 查询ADE Dataset列表 */
    getList() {
      this.loading = true;
      listEmr(this.queryParams).then(response => {
        this.emrList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        content: null,
        year: null,
        month: null,
        type: null,
        ades: null,
        adesGpt: null,
        adesEnire: null,
        adesQwen: null,
        adesDeepseek: null,
        status: null,
        source: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "Add";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getEmr(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "Edit";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateEmr(this.form).then(response => {
              this.$modal.msgSuccess("Edit successfully!");
              this.open = false;
              this.getList();
            });
          } else {
            addEmr(this.form).then(response => {
              this.$modal.msgSuccess("Add successfully!");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除ADE Dataset编号为"' + ids + '"的数据项？').then(function () {
        return delEmr(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ade/emr/export', {
        ...this.queryParams
      }, `emr_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
